<?php
	class ReportCardController
	{
		private $d;
		public function __construct()
		{
			$this->d = new DbHelper();
		}
		public final function getAllPeriodicalTypes()
		{
			$s = "SELECT * FROM PeriodicalTypes";
			return $this->d->query($s);
		}
		public final function getGradePerSubject($subjectName, 
													$studentId, 
													$schoolYear)
		{
			$i = "(SELECT Id FROM Subjects WHERE Name = '$subjectName')";
			$sy = "(SELECT Id FROM SchoolYears WHERE Year = '$schoolYear')";
			$s = 	"SELECT PeriodicalTypeId, Grade FROM Grades WHERE 
					StudentId = $studentId AND SubjectId = $i AND SchoolYearId = $sy 
					ORDER BY PeriodicalTypeId";
			return $this->d->query($s);
		}
		/*
			@param		$grades		A multi-dimensional array 
			of subject IDs that hold the grades.
			$grades[123][] = 90
			$grades[123][] = 80
			$grades[456][] = 95
		*/
		public final function updateGrades($studentId, $schoolYear, $grades)
		{
			$r = $this->getAllPeriodicalTypes();
			while($o = mysql_fetch_object($r))
			{
				$g = $grades;
				foreach ($g as $subjectId => $value) 
				{
					$values = $g[$subjectId];
					$i = count($values);
					for($a = 0; $a < $i; $a++)
					{
						$this->updateGrade($studentId, 
											$schoolYear, 
											$subjectId, 
											$o->Id, 
											$values[$a]);
					}
				}
			}
		}
		private final function updateGrade($studentId, 
											$schoolYear, 
											$subjectId, 
											$periodicalTypeId, 
											$grade)
		{
			$sy = "(SELECT Id FROM SchoolYears WHERE Year = '$schoolYear')";
			$s = "UPDATE Grades SET Grade = $grade 
			      WHERE StudentId = $studentId AND SchoolYearId = $sy AND 
			      SubjectId = $subjectId AND 
				  PeriodicalTypeId = $periodicalTypeId";
			return $this->d->query($s);
		}
		/*
			Call this method upon enrolment to initially create 
			grades with values $defaultGrade.  If the student is 
			already enroled	and you want to set all his grades at 
			$defaultGrade, then use updateGrade() instead.
		*/
		public final function createDefaultGrades($studentId, $schoolYear)
		{
			$sc = new StudentController();
			if($sc->getStudentIsEnroled($studentId, $schoolYear)) 
				return;
			//
			const DEFAULT_GRADE = 75;
			$sc = new SubjectController();
			$rSubjs = $sc->getAllSubjects();
			$rPrdclTypes = $this->getAllPeriodicalTypes();
			while($oPrdclType = mysql_fetch_object($rPrdclTypes))
			{
				while($oSubj = mysql_fetch_object($rSubjs))
				{
					$this->createGrade($studentId, 
											$schoolYear, 
											$oSubj-Id, 
											$oPrdclType->Id, 
											DEFAULT_GRADE);
				}
			}
		}
		private final function createGrade($studentId, 
											$schoolYear, 
											$subjectId, 
											$periodicalTypeId, 
											$grade)
		{
			$sy = "(SELECT Id FROM SchoolYears WHERE Year = '$schoolYear')";
			$s = "INSERT INTO Grades (StudentId, SchoolYearId, 
					SubjectId, PeriodicalTypeId, Grade)
					VALUES ('$studentId', $sy, '$subjectId', 
					'$periodicalTypeId', '$grade')";
			return $this->d->query($s);
		}
	}
?>